/*
 * validateCost.js
 * v 1.3
 * 4/03/2011
 * 
 * This file validates the cost input.
 */
 	
	//This function checks if the input is a number.
	function isNumber(str){
		//Regex pattern for number.
		var isNumExp = /^[0-9.]+$/;
		//Return
		return isNumExp.test(str);
	}
	

	//This function confirms the submission.
	function confirmSubmission(){
		var answer = confirm("Are you sure you want to save changes?");
		return answer;
	}
	
	//This function basically puts the main type names into an array. 
	function checkCostFields(str){ 
		//Initialize array variable.
		var sample=new Array();
		//Assign each main type name to an array index.
		sample[0]="Fertilizer Analysis";
		sample[1]="Carbohydrate Analysis";
		sample[2]="Fats and Oils";
		sample[3]="Mineral Analysis";
		sample[4]="Proximate Analysis";
		sample[5]="Gas Chromatographic Analysis: Alcohols";
		sample[6]="Gas Chromatographic Analysis: Fatty Acid Profile";
		sample[7]="HPLC Analysis";
		sample[8]="HPLC Analysis: Plant Hormones";
		sample[9]="HPLC Analysis: Sugars";
		sample[10]="XRF Elemental Analysis";
		sample[11]="Packaging Material Testing";
		sample[12]="Soil Analysis";
		sample[13]="Soil Analysis: Exchangeable Cations";
		sample[14]="Soil Analysis: Micronutrients (Fe, Cu, Mn, Zn)	";
		sample[15]="Water Analysis";
		sample[16]="Water Analysis: Arsenic and Mercury";
		sample[17]="Water Analysis: Elements";
		
		//Initialize array variable.
		var samplestart = new Array();
		var sampleend = new Array();
		//Assign values.
			samplestart[0]=0;sampleend[0]=8;
			samplestart[1]=8;sampleend[1]=11;
			samplestart[2]=11;sampleend[2]=16;
			samplestart[3]=16;sampleend[3]=20;
			samplestart[4]=20;sampleend[4]=27;
			samplestart[5]=27;sampleend[5]=30;
			samplestart[6]=30;sampleend[6]=33;
			samplestart[7]=33;sampleend[7]=36;
			samplestart[8]=36;sampleend[8]=39;
			samplestart[9]=39;sampleend[9]=42;
			samplestart[10]=42;sampleend[10]=43;
			samplestart[11]=43;sampleend[11]=48;
			samplestart[12]=48;sampleend[12]=59;
			samplestart[13]=58;sampleend[13]=61;
			samplestart[14]=61;sampleend[14]=63;
			samplestart[15]=63;sampleend[15]=87;
			samplestart[16]=0;sampleend[16]=8;
			samplestart[17]=0;sampleend[17]=8;
		
		for(i=0;i<18;i++){
			if(sample[i]==str){
				return checkEmptyFields(samplestart[i],sampleend[i]);
			}
		}
	
	}
	
	//This function checks if the fields are empty or has an invalid input.
	function checkEmptyFields(start,end){
		//Initialize variables.
		var current;
		var i;
		var flag = 0;
		var flag1 = 0;
		
		
		for(i=start;i<end;i++){
			switch(i){
			
			//Fertilizer Analysis 
				//Fertilizer Analysis
				case 0: current = "Chloride Per Sample Cost"; break;
				case 1: current = "Heavy Metals Per Element Cost"; break;
				case 2: current = "Macroelements Per Element Cost"; break; 
				case 3: current = "Micronutrients Per Element Cost"; break;
				case 4: current = "Nitrogen (N) Cost"; break; 
				case 5: current = "Phosphorous (P) Cost"; break;
				case 6: current = "Potassium (K) Cost"; break;
				case 7: current = "Sample Preparation Per Sample Cost"; break;
			//Food and Feed	
				//Carbohydrate Analysis
				case 8: current = "Extraction Cost"; break;
				case 9: current = "Reducing Sugars (DNS) Cost"; break;
				case 10: current = "Total Sugars Cost"; break;
				//Fats and Oils
				case 11: current = "Free Fatty Acid Cost"; break;
				case 12: current = "Iodine Number Cost"; break;
				case 13: current = "Peroxide Value Cost"; break;
				case 14: current = "Saponification Number Cost"; break;
				case 15: current = "Specific Gravity Cost"; break;
				//Mineral Analysis
				case 16: current = "Minerals/Metals Cost"; break;
				case 17: current = "Percent Salts Cost"; break;
				case 18: current = "Sample Preparation Cost"; break;
				case 19: current = "Total Phosphorous Cost"; break;
				//Proximate Analysis
				case 20: current = "Ash Cost"; break;
				case 21: current = "Crude Fat Cost"; break;
				case 22: current = "Crude Fiber Cost"; break;
				case 23: current = "Crude Protein Cost"; break;
				case 24: current = "Moisture Cost"; break;
				case 25: current = "Proximate Analysis(Complete) Cost"; break;
				case 26: current = "Sample Preparation Cost"; break;
			//Instrumental Analysis
				//Gas Chromatographic Analysis: Alcohols
				case 27: current = "Ethanol Cost"; break;
				case 28: current = "Methanol Cost"; break;
				case 29: current = "Sample Preparation Cost"; break;
				//Gas Chromatographic Analysis: Fatty Acid Profile
				case 30: current = "Instrumentation Cost"; break;
				case 31: current = "Optimization Cost"; break;
				case 32: current = "Sample Preparation Cost"; break;
				//HPLC Analysis
				case 33: current = "G+C Cost"; break;
				case 34: current = "Vitamins A Cost"; break;
				case 35: current = "Vitamins C Cost"; break;
				//HPLC Analysis: Plant Hormones
				case 36: current = "Instrumentation Cost"; break;
				case 37: current = "Optimization Cost"; break;
				case 38: current = "Sample Preparation Cost"; break;
				//HPLC Analysis: Sugars
				case 39: current = "Instrumentation Cost"; break;
				case 40: current = "Optimization Cost"; break;
				case 41: current = "Sample Preparation Cost"; break;
				//XRF Elemental Analysis
				case 42: current = "XRF Elemental Analysis Cost"; break;
			//Packaging Material Testing	
				//Packaging Material Testing
				case 43: current = "Metals (Pb and Cd) Cost"; break;
				case 44: current = "Packaging Material Testing (Complete) Cost"; break;
				case 45: current = "Residues Cost"; break;
				case 46: current = "Sample Preparation Cost"; break;
				case 47: current = "UV Examination Cost"; break;
			//Soil Analysis	
				//Soil Analysis
				case 48: current = "Ammonium N Cost"; break;
				case 49: current = "Available N Cost"; break;
				case 50: current = "Available Phosphorous (Bray P2) Cost"; break;
				case 51: current = "Cation Exchange Capacity Cost"; break;
				case 52: current = "Moisture Cost"; break;
				case 53: current = "Nitrates Cost"; break;
				case 54: current = "Organic Matter (Walkley and Black) Cost"; break;
				case 55: current = "pH (in 0.01M CaCl2) Cost"; break;
				case 56: current = "pH (in Water) Cost"; break;
				case 57: current = "Sample Preparation (Homogenization) Cost"; break;
				case 58: current = "Total Kjeldahl Nitrogen Cost"; break;
				//Soil Analysis: Exchangeable Cations
				case 59: current = "Instrumentation (AAS) Cost"; break;
				case 60: current = "Sample Preparation Cost"; break;
				//Soil Analysis: Micronutrients (Fe, Cu, Mn, Zn)
				case 61: current = "Instrumentation (AAS) Cost"; break;
				case 62: current = "Sample Preparation Cost"; break;
			//Water Analysis	
				//Water Analysis
				case 63: current = "Absorbance Cost"; break;
				case 64: current = "Acidity Cost"; break;
				case 65: current = "Alkalinity Cost"; break;
				case 66: current = "Ammonium-N Cost"; break;
				case 67: current = "Biological Oxygen Demand (BOD) Cost"; break;
				case 68: current = "Chemical Oxygen Demand (COD) Cost"; break;
				case 69: current = "Chlorides Cost"; break;
				case 70: current = "Color Cost"; break;
				case 71: current = "Conductivity Cost"; break;
				case 72: current = "Dissolved Oxygen Cost"; break;
				case 73: current = "Nitrate-N Cost"; break;
				case 74: current = "Nitrite-N Cost"; break;
				case 75: current = "Oil and Grease Cost"; break;
				case 76: current = "Orthophosphates Cost"; break;
				case 78: current = "pH Cost"; break;
				case 79: current = "Total Dissolved Solids Cost"; break;
				case 80: current = "Total Hardness Cost"; break;
				case 81: current = "Total Kjeldahl N Cost"; break;
				case 82: current = "Total Phosphorous Cost"; break;
				case 83: current = "Total Residual Chloride Cost"; break;
				case 84: current = "Total Solids Cost"; break;
				case 85: current = "Total Suspended Solids Cost"; break;
				case 86: current = "Turbidity Cost"; break;
				//Water Analysis: Arsenic and Mercury
				case 87: current = "Instrumentation (AAS Method) Cost"; break;
				case 88: current = "Sample Preparation Cost"; break;
				//Water Analysis: Elements
				case 89: current = "Instrumentation (AAS Method) Cost"; break;
				case 90: current = "Sample Preparation Cost"; break;
			}
			//Calls a function to check if field is blank.
			if((isBlank(document.getElementById(current).value))){
				//Flag if the field is blank.
				flag++;
			}
			//Calls a function to check if input is a number.
			else if((!isNumber(document.getElementById(current).value))){
				//Flag if the input is not a number.
				flag1++;
			}
		}
		/* Call the function to confirm submission if all text fields are not null
		  and all inputs are number. */
		if(flag == 0 && flag1 == 0){
			return confirmSubmission();
		}
		//Alert if empty field(s) or invalid input(s) is/are detected.
		if(flag > 0)
			alert("There is/are empty field(s).");
		if(flag1 > 0)
			alert("There is/are invalid input(s).");
		return false;
		
	}
	
	//This function checks if input is a number
	function validDecimal( input ) {
		//Initialize variables.
		var msg = input.value; 
		var w;
		var decimal = msg.indexOf( "." );
		var index;
	  
		for ( w=0; w<msg.length; w++ )  {

		  index = msg.substring(w,w+1);
		  if ( index<"0" || index>"9" )  {
		
			   if ( decimal>0 )
				 if ( w==decimal )  continue;    
		   
			   msg = msg.substring(0,w);
			   input.value = msg; 
			   break;           
		  }

		}
	}
	
	//This function checks if the field is blank or null
	function isBlank(fieldValue){	
		if(fieldValue == "" || fieldValue == null){				
			return true;
		}
		return false;
	}